package gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;

import javax.swing.JPanel;

import system.SystemProperties;

/**
 * @author Kevin Peters
 */
public class MaskGrid extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = -6950358120440212771L;

	private LinkedList<DrawingPanel> drawingPanelList = new LinkedList<DrawingPanel>();
	private Map<Integer,DrawingPanel> drawingPanelMap = new HashMap<Integer,DrawingPanel>();

	private Color drawingColor;

	public MaskGrid() {
		setLayout(new GridLayout(SystemProperties.CANVASROWS,
				SystemProperties.CANVASCOLS, SystemProperties.CANVASHGAP,
				SystemProperties.CANVASVGAP));
		setVisible(true);
		setPreferredSize(new Dimension(SystemProperties.CANVASWIDTH,
				SystemProperties.CANVASHEIGHT));
	}

	/**
	 * Adds the given drawing panel to the panel list
	 * 
	 * @param drawingPanel
	 */
	public void announceDrawingPanel(DrawingPanel drawingPanel) {
		drawingPanelList.add(drawingPanel);
	}
	
	/**
	 * Generates a mapping of DrawingPanels and its grid index from source image
	 */
	public void mapDrawingPanels(){
		for (DrawingPanel currentPanel : drawingPanelList) {
			int sourceId = SystemProperties.PANELMAPPING_EDITOR2SOURCE.get(currentPanel.getId());
			drawingPanelMap.put(sourceId, currentPanel);
		}
		
	}

	/**
	 * Sets the given drawing color to all panels in the grid
	 * 
	 * @param drawingColor
	 */
	public void noticeAllPanels(Color drawingColor) {
		setDrawingColor(drawingColor);
		for (DrawingPanel currentPanel : drawingPanelList) {
			currentPanel.setDrawingColor(drawingColor);
		}
	}
	
	/**
	 * Iterates all panels and sets the localized tooltip
	 * 
	 * @param systemProperties
	 */
	public void documentAllPanels(SystemProperties systemProperties) {
		for (DrawingPanel currentPanel : drawingPanelList) {
			String toolTipText = systemProperties.getToolTipText(currentPanel.getId());
			currentPanel.setToolTipText("<html>"+toolTipText+"</html>");
		}
	}

	public Color getDrawingColor() {
		return drawingColor;
	}

	public void setDrawingColor(Color drawingColor) {
		this.drawingColor = drawingColor;
	}

	public LinkedList<DrawingPanel> getDrawingPanelList() {
		return drawingPanelList;
	}

	public void setDrawingPanelList(LinkedList<DrawingPanel> drawingPanelList) {
		this.drawingPanelList = drawingPanelList;
	}

	public Map<Integer, DrawingPanel> getDrawingPanelMap() {
		return drawingPanelMap;
	}
}
